package com.aaa.doctor.service.impl;

import com.aaa.doctor.feign.system.UserFeign;
import com.aaa.doctor.mapper.CareOrderItemMapper;
import com.aaa.doctor.mapper.CareOrderMapper;
import com.aaa.doctor.service.CareOrderService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.doctor.vo.CareOrderVo;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.entity.User;
import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author MuChengGuo
 * @date 2023/3/25 10:22
 * Description: TODO
 **/
@Service
public class CareOrderServiceImpl implements CareOrderService {
    @Autowired(required = false)
    private CareOrderMapper careOrderMapper;
    @Autowired(required = false)
    private CareOrderItemMapper careOrderItemMapper;
    @Autowired
    private UserFeign userFeign;

    /**
     * 通过患者ID(patientId)查询患者药用处方信息&检查和用药项目明细
     *
     * @param patientId
     * @return
     */
    @Override
    public Result<List<CareOrder>> getListByPatientId(String patientId) {
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patientId);
        List<CareOrder> careOrders = careOrderMapper.selectList(wrapper);

        return new Result<>(2000, "通过患者ID(patientId)查询患者药用处方信息", careOrders);
    }

    @Override
    public Result<String> saveCareOrderItem(CareOrderVo careOrderVo) {
        // 药用处方表
        CareOrder careOrder = careOrderVo.getCareOrder();
        // 开诊明细表
        List<CareOrderItem> careOrderItems = careOrderVo.getCareOrderItems();
        // 当前医生的全部信息
        String phone = (String) JwtUtil.getTokenChaim(WebUtil.getRequest().getHeader("token")).get("username");
        User byUsername = userFeign.getByUsername(phone);

        Long idWorker = new IdWorker().nextId();
        int i = new Random().nextInt(89) + 10;
        StringBuilder co = new StringBuilder("CO").append(idWorker).append(i);
        careOrder.setCoId(String.valueOf(co));
        careOrder.setUserId(byUsername.getUserId());
        careOrder.setCreateBy(byUsername.getUserName());
        careOrder.setCreateTime(new Date());
        // 添加药用处方表
        careOrderMapper.insert(careOrder);

        for (CareOrderItem careOrderItem : careOrderItems) {
            Long idWorker1 = new IdWorker().nextId();
            int i1 = new Random().nextInt(89) + 10;
            StringBuilder ca = new StringBuilder("ITEM").append(idWorker1).append(i1);
            careOrderItem.setItemId(String.valueOf(ca));
            careOrderItem.setCoId(String.valueOf(co));
            careOrderItem.setStatus("0");
            careOrderItem.setCreateTime(new Date());
            careOrderItemMapper.insert(careOrderItem);
        }
        return new Result<>(2000, "成功", null);
    }

    @Override
    public Result<List<CareOrder>> queryCareOrdersByChId(String chId) {
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderMapper.selectList(wrapper);
        for (CareOrder careOrder : careOrders) {
            QueryWrapper<CareOrderItem> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("co_id", careOrder.getCoId());
            List<CareOrderItem> items = careOrderItemMapper.selectList(wrapper1);
            careOrder.setCareOrderItemList(items);
        }
        return new Result<List<CareOrder>>(2000, "成功", careOrders);
    }

    @Override
    public Result<List<CareOrder>> getCareHistoryByChId(String chId) {
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<CareOrder>().eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderMapper.selectList(wrapper);

        for (CareOrder careOrder : careOrders) {
            QueryWrapper<CareOrderItem> wrapper1 = new QueryWrapper<CareOrderItem>().eq("co_id", careOrder.getCoId());
            List<CareOrderItem> items = careOrderItemMapper.selectList(wrapper1);
            careOrder.setCareOrderItemList(items);
        }
        return new Result<List<CareOrder>>(2000, "成功", careOrders);
    }
}
